package com.swxc.smartorder.ui.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.swxc.smartorder.R;
import com.swxc.smartorder.network.NetworkHelper;
import com.swxc.smartorder.utils.DisplayUtil;

/**
 * Author   Administrator
 * Date     2018/3/29
 * Desc     退出登录对话框
 *
 * @author Administrator
 */
public class SignOutDialog extends DialogFragment implements View.OnClickListener {
    public static boolean isShow = false;
    private Activity mActivity;
    private TextView mTvSure;
    private TextView mTvCancel;
    private NetworkHelper mNetworkHelper;
    private TextView mTvTitle;
    private TextView mTvContent;
    private String strTitle;
    private String strContent;
    private String strSure;
    private String strCancel;

    private ClickListener mClickListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShow = true;
        mActivity = this.getActivity();
        setStyle(android.support.v4.app.DialogFragment.STYLE_NO_TITLE, R.style.PromptDialog);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }


    @Override
    public void onStart() {
        super.onStart();
        //点击window外的区域 是否消失
        int margin = DisplayUtil.INSTANCE.dip2px(mActivity, 0);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        assert window != null;
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(margin, 0, margin, margin);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return null;
        }
        strTitle = bundle.getString("strTitle");
        strContent = bundle.getString("strContent");
        strSure = bundle.getString("strSure");
        strCancel = bundle.getString("strCancel");


        return inflater.inflate(R.layout.layout_sign_out, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        mTvSure = view.findViewById(R.id.tv_sure);
        mTvCancel = view.findViewById(R.id.tv_cancel);
        mTvTitle = view.findViewById(R.id.tv_title);
        mTvContent = view.findViewById(R.id.tv_content);
        mTvCancel.setOnClickListener(this);
        mTvSure.setOnClickListener(this);
        mTvTitle.setText(strTitle);
        mTvContent.setText(strContent);
        mTvSure.setText(strSure);
        mTvCancel.setText(strCancel);
        mNetworkHelper = new NetworkHelper(mActivity, getFragmentManager());

    }

    @Override
    public void onDismiss(DialogInterface dialog) {

        super.onDismiss(dialog);
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sure:
                if (mClickListener != null) {
                    mClickListener.sureOnClickListener();
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setOnSureClickListener(ClickListener listener) {
        mClickListener = listener;

    }

    public interface ClickListener {
        /**
         * 点击确定
         */
        void sureOnClickListener();
    }
}
